//
//  AlertViewAutohideViewController.m
//  UITest
//
//  Created by wp w on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AlertViewAutohideViewController.h"
#import "DLog.h"

@interface AlertViewAutohideViewController ()

@end

@implementation AlertViewAutohideViewController

NSTimer *timer;

- (void)hideAlert:(NSTimer *)sender
{
	UIAlertView *alert = [sender userInfo];
	
	[alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)viewDidLoad {
	
	[super viewDidLoad];
	
    //创建一个5秒后自动消失的，且可点击的对话框
	UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"title" 
													  message:@"This alert will disappear in 5 seconds" 
													 delegate:self 
											cancelButtonTitle:@"OK" 
											otherButtonTitles:nil];
	
	[myAlert addButtonWithTitle:@"new button"];
	[myAlert addButtonWithTitle:@"another button"];
	
    //对话框显示
	[myAlert show];
	
    //设置一个定时器，5秒后执行
	//create a timer to hide the alert automatically in 5 seconds
	timer = [[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(hideAlert:) userInfo:myAlert repeats:NO] retain];
	
	[myAlert release];	
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:     //特别注意，函数中默认的那个btn的索引为0
            LogStr(@"OK");
            break;
        case 1:
            LogStr(@"new button");
            break;
        case 2:
            LogStr(@"another button");
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    
	[timer invalidate];
	[timer release];
	timer = nil;
    
    [super dealloc];
}
@end
